package cn.neo.support.smartadapters.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.neo.support.smartadapters.utils.ViewEventListener;

/**
 * <b>Name:</b>xx页面<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b>  2017-03-09<br>
 * <b>Describe:</b><p> PLEASE TO FIXME...</p><br>
 * <p>代码说明：（重点）<br>
 * <ol>
 * <li> write something content..</li>
 * </ol>
 * </p>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b>
 * <ul>
 * v1.x<li>1.write something content..。<br>2.write something content..<br>3.write something content..<br></li>
 * </ul>
 * <br>
 */
public abstract class BindableImageView<T> extends ImageView implements BindableLayout<T> {

    protected ViewEventListener<T> viewEventListener;
    protected T item;
    protected int position;

    public BindableImageView(Context context) {
        super(context);
    }

    public BindableImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BindableImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initImageView();
    }


    public void setLayoutParams(int w, int h) {
//        ViewGroup.LayoutParams.MATCH_PARENT
//        ViewGroup.LayoutParams.WRAP_CONTENT
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(w, h);
        setLayoutParams(layoutParams);
    }

    @Override
    public void bind(T item, int position) {
        this.item = item;
        this.position = position;
        bind(item);
    }

    public abstract void initImageView();

    @Override
    public abstract void bind(T item);

    @Override
    @Nullable
    public ViewEventListener<T> getViewEventListener() {
        return viewEventListener;
    }

    @Override
    public void setViewEventListener(ViewEventListener<T> viewEventListener) {
        this.viewEventListener = viewEventListener;
    }

    @Override
    public void notifyItemAction(int actionId, T theItem, View view) {
        if (viewEventListener != null) {
            viewEventListener.onViewEvent(actionId, theItem, position, view);
        }
    }

    public void notifyItemAction(int actionId, View view) {
        notifyItemAction(actionId, item, view);
    }

    public void notifyItemAction(int actionId) {
        notifyItemAction(actionId, item, this);
    }

    public T getItem() {
        return item;
    }

    public void setItem(T item) {
        this.item = item;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
}
